package com.bsdsma.fengheguaiapp.restcontroller;

import com.bsdsma.fengheguaiapp.dto.RoominfoAddDto;
import com.bsdsma.fengheguaiapp.dto.RoominfoSearchDto;
import com.bsdsma.fengheguaiapp.dto.DataGroupByDateDto;
import com.bsdsma.fengheguaiapp.service.RoominfoService;
import com.bsdsma.fengheguaiapp.utils.ResultUtils;
import com.bsdsma.fengheguaiapp.utils.DateUtils;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 酒店房型模块对外暴露api入口, 即可通过http请求访问到下面各个方法
 */
@RestController
@RequestMapping(value = "/api/roominfo")
@Tag(name = "RoominfoController", description = "酒店房型管理")
public class RoominfoRestController {

    @Autowired
    private RoominfoService roominfoService;

    /**
     * 分页查询酒店房型, 接收"HTTP GET"请求
     * @param search    入参, 查询条件对象
     */
    @GetMapping
    public Map<String, Object> search(RoominfoSearchDto search){
        return ResultUtils.ok(roominfoService.search(search));
    }

    /**
    * 查询所有酒店房型, 接收"HTTP GET"请求
    */
    @GetMapping(value = "/list")
    public Map<String, Object> listAll(){
        return ResultUtils.ok(roominfoService.listAll());
    }

    /**
    * 通过主键ID查询酒店房型, 接收"HTTP GET"请求
    */
    @GetMapping(value = "/{id}")
    public Map<String, Object> getById(@PathVariable Long id){
        return ResultUtils.ok(roominfoService.getById(id));
    }

    /**
     * 添加酒店房型数据到数据库, 接收"HTTP POST"请求
     * @param entity    入参, 添加的字段对象
     */
    @PostMapping
    public Map<String, Object> add(@RequestBody RoominfoAddDto entity){
        System.out.println("接收到的数据：" + entity);  // 添加这行
        roominfoService.add(entity);
        return ResultUtils.ok();
    }

    /**
     * 更新酒店房型数据到数据库, 接收"HTTP POST"请求
     * @param id        主键ID, 更新需要一个唯一标识, 也就是主键ID
     * @param entity    入参, 更新的字段对象
     */
    @PostMapping(value = "/{id}")
    public Map<String, Object> update(@PathVariable Long id, @RequestBody RoominfoAddDto entity){
        roominfoService.update(id, entity);
        return ResultUtils.ok();
    }

    /**
     * 删除酒店房型数据, 接收"HTTP DELETE"请求
     * @param id    主键ID, 删除与更新一样, 也需要一个唯一标识
     */
    @DeleteMapping(value = "/{id}")
    public Map<String, Object> delete(@PathVariable Long id){
        roominfoService.delete(id);
        return ResultUtils.ok();
    }

    /**
    * 查询酒店房型数量, 接收"HTTP GET"请求
    */
    @GetMapping(value = "/count")
    public Map<String, Object> count(){
        Long count = roominfoService.count();
        return ResultUtils.ok(null == count ? 0L : count);
    }

    /**
    * 日新增统计
    */
    @GetMapping(value = "/day-newly-added")
    public Map<String, Object> dayNewlyAdded(){
        // 今天
        Date endDate = DateUtils.getNow();
        // 30天以前
        Date beginDate = DateUtils.dayAfter(endDate, -30);
        List<DataGroupByDateDto> dataList = roominfoService.countByDay(beginDate, endDate);
        return ResultUtils.ok(dataList);
    }
}
